package com.hiwan.system.dao.org;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.framework.dao.ServiceUtilDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.hiwan.system.pojo.OrgInfo;

@Component
@Service
@Repository
public class OrgDao extends ServiceUtilDao {
	/***
	 * 根据机构号获取机构信息
	 * 
	 * @param orgId
	 * @return
	 */
	public OrgInfo selectOrgInfo(String orgId) {
		return selectOne("selectOrgInfo", orgId, OrgInfo.class);
	}
	public OrgInfo selectOneByOrgCode(String orgId) {
		return selectOne("selectOneByOrgCode", orgId, OrgInfo.class);
	}
	public OrgInfo selectOrgByAreaCode(String areaCode) {
		return selectOne("selectOrgByAreaCode", areaCode, OrgInfo.class);
	}
	public List<OrgInfo> getOrgListWithTree(OrgInfo orgInfo) {
		return selectList("selectOrgListWithTree", orgInfo, OrgInfo.class);
	}
	/**
	 * 初始化时加载全部机构到缓存
	 * @return
	 */
	public List<OrgInfo> getOrgListInit() {
		return selectList("selectOrgListInit", null, OrgInfo.class);
	}

	/**
	 * 根据orgid查询二级市分行信息
	 * @return
	 */
	public List<OrgInfo> getOrgSupperInfo(String orgId) {
		return selectList("selectSupperOrgInfo", orgId, OrgInfo.class);
	}
	public void insertOrgInfo(OrgInfo orgInfo) {
		
		insert("insertOrgInfo",orgInfo);
		updateOrgHasChild(orgInfo.getParentId());
	}
	public void updateOrgHasChild(String parentId){
		update("updateParentOrg1", parentId);
	}

	public void updateOrgInfo(OrgInfo orgInfo) {
		insert("updateOrgInfo",orgInfo);
	}

	public void deleteOrgInfo(OrgInfo orgInfo) {
		delete("deleteOrgInfo",orgInfo);
		update("updateParentOrg0", orgInfo.getParentId());
	}
	
	
	/**
	 * 根据newOrgId查询oldOrgId列表
	 * @param newOrgId
	 * @return
	 */
	public List<String> getOldOrgIdListByNewOrgId(String newOrgId){
		OrgInfo info = selectOne("selectOldOrgIdList", newOrgId, OrgInfo.class);
		return info.getOldOrgIdList();
	}
	/**
	 * 根据olbOrgId查询newOrgId
	 * @param oldOrgId
	 * @return
	 */
	public String getNewOrgIdByOldOrgId(String oldOrgId){
		OrgInfo info = selectOne("selectNewOrgtId", oldOrgId, OrgInfo.class);
		if(info == null){
			return null;
		}
		return info.getOrgId();
	}
	
	public List<OrgInfo> getOrgTreeForCustomize(String mapId, Object parameter){
		int idx = mapId.lastIndexOf('.');
		String mapper = mapId.substring(0, idx);
		String statement = mapId.substring(idx + 1);
		return selectList(mapper, statement, parameter, OrgInfo.class);
	}

	/**
	 * 根据给定机构编码查统计查询机构树
	 * @param info
	 * @return
	 */
	public List<OrgInfo> getOrgList4Report(String orgId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		return selectList("selectChildList4Report", map, OrgInfo.class);
	}
	
	/***
	 * 根据机构号获取机构信息
	 * 
	 * @param areaCode
	 * @return
	 */
	public OrgInfo selectVirtualOrgInfo(String orgId) {
		return selectOne("selectVirtualOrgInfo", orgId, OrgInfo.class);
	}
	
	public List<OrgInfo> selectOnlyChildOrgList(String orgId, boolean isContainSelf) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		map.put("isContainSelf", String.valueOf(isContainSelf));
		return selectList("selectOnlyChildOrgList", map, OrgInfo.class);
		
	}

	public OrgInfo selectRootOrgInfo() {
		return selectOne("selectRootOrg",null, OrgInfo.class);
	}

	public int updateOrgLevelOrder(OrgInfo orgInfo) {
		return update("updateLevelOrder", orgInfo);
		
	}
	public int updateOtherOrgLevelOrder(String parentId, int maxLevelOrder, int minLevelOrder, int step) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", parentId);
		map.put("maxLevelOrder", maxLevelOrder);
		map.put("minLevelOrder", minLevelOrder);
		map.put("step", step);
		return update("updateOtherLevelOrder", map);
		
	}
	
	public List<OrgInfo> selectOrgList(OrgInfo orgInfo){
		return selectList("selectOrgInfoByEntry", orgInfo, OrgInfo.class);
	}
	
	public int insertOrgInfoFromXXZ(OrgInfo orgInfo){
		return insert("insertOrgInfoFromXXZ", orgInfo); 
	}
	public int updateOrgInfoFromXXZ(OrgInfo orgInfo){
		return insert("updateOrgInfoFromXXZ", orgInfo); 
	}
	public int deleteOrg(OrgInfo orgInfo){
		return delete("deleteOrg", orgInfo);
	}
	/**
	 * 区域
	 * @param orgInfo
	 * @return
	 */
//	public List<OrgInfo> getOrgRegion(OrgInfo orgInfo){
//		return this.selectList("getOrgRegion", orgInfo, OrgInfo.class);
//	}
	/**
	 * 支行
	 * @param orgInfo
	 * @return
	 */
//	public List<OrgInfo> getOrgBranch(OrgInfo orgInfo){
//		return this.selectList("getOrgBranch", orgInfo, OrgInfo.class);
//	}

	public List<OrgInfo> selectOrgInfoByProductId(OrgInfo orgInfo){
		return selectList("selectOrgInfoByProductId", orgInfo, OrgInfo.class);
	}
}